package cn.edu.ujn.lab3.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.IUserService;
import cn.edu.ujn.lab3.util.MD5Encoder;

/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private IUserService userService;

	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode, String password, Model model, HttpSession session) {
		MD5Encoder pr = new MD5Encoder();
		// 通过账号和密码查询用户
		User user;
		try {
			user = userService.findUser(usercode, pr.geMd5("abcdef", password));  
			if (user != null) {
				// 将用户对象添加到Session
				session.setAttribute("USER_SESSION", user); 
				// 跳转到主页面
//				return "customer";
				return "redirect:customer/list.action";
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/*
		 * if(user != null){ // 将用户对象添加到Session session.setAttribute("USER_SESSION",
		 * user); // 跳转到主页面 // return "customer"; return
		 * "redirect:customer/list.action"; }
		 */
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
		// 返回到登录页面
		return "login";
	}

	@RequestMapping(value = "/register.action")
	public String register(String username, String usercode, String password, HttpServletRequest request,
			HttpServletResponse response) {

		MD5Encoder pr = new MD5Encoder();
		User user = new User(); 
		user.setUserName(username); 
		user.setUserCode(usercode); 
		// 检验准备注册的用户账号是否存在，如果账号存在，则返回注册页面重新注册

		if (userService.selectByCode(usercode) != null) {

			request.setAttribute("msg", "账号已存在，请重新注册！");

			// 跳转到注册页面
			return "forward:toRegister.jsp";
		}
		try {
			// 使用MD5对用户密码进行加密
			user.setUserPassword(pr.geMd5("abcdef", password));

			user.setUserState(1);
			// 向数据库里添加用户
			userService.register(user);
			return "redirect:login.action";
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "forward:toRegister.jsp";
	}

	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
		// 清除Session
		session.invalidate();
		// 重定向到登录页面的跳转方法     
		return "redirect:login.action";
	}

	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
		return "login";
	}

	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toCustomer.action")
	public String toCustomer() {
		return "customer";
	}

}